﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Process
{
    public class SearchProcessModel
    {
        [DisplayName("Buscar por Nombre")]
        [StringLength(50, ErrorMessage = "El nombre debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras mayusculas o minusculas o numeros")]
        public string Pattern { get; set; }

        public List<SearchProcessInfo> ProcessInfo
        {
            get
            {
                return processInfo;
            }
        }

        private List<SearchProcessInfo> processInfo;

        public SearchProcessModel()
        {
            SearchManagement();
        }

        public void SearchManagement(String pattern)
        {
            riskanalysisEntities entities = new riskanalysisEntities();
            var managements = from p in entities.processes
                             where p.name.Contains(pattern) && p.enabled
                             select new {
                                 ProcessId = p.processId,
                                 Name = p.name,                                  
                                 Management = p.management.name};
            processInfo = new List<SearchProcessInfo>();

            foreach (var man in managements)
            {
                processInfo.Add(
                    new SearchProcessInfo
                    {
                        ProcessId = man.ProcessId,
                        Name = man.Name,                        
                        Management = man.Management
                    });
            }  
            
        }

        public void SearchManagement()
        {
            SearchManagement("");
        }
    }
}